package com.apesource.mallproject.dao;

import com.apesource.mallproject.pojo.Advertising;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;

@Repository
public interface IAdDao {
    //查询所有广告
    @Select("select * from content order by id desc")
    public List<Advertising> queryAll();

    //新增广告
    @Insert("INSERT INTO content VALUES(NULL,#{ad.categoryId},#{ad.title},#{ad.url},#{ad.pic},#{ad.status},#{ad.sortOrder})")
    public void insertAd(@Param("ad") Advertising ad);

    //修改广告
    @Update("update content set category_id=#{ad.categoryId},title=#{ad.title},url=#{ad.url},pic=#{ad.pic},status=#{ad.status}," +
            "sort_order=#{ad.sortOrder} where id = #{ad.id}")
    public void updateAd(@Param("ad") Advertising ad);

    //删除一个
    @Delete("delete from content where id = #{id}")
    void delAd(@Param("id") BigInteger id);

    //批量删除
    @DeleteProvider(type = IAdDao.AdProvider.class, method = "batchDelete")
    int batchDeleteAd(String[] ids);

    class AdProvider {
        public String batchDelete(Map map) {
            String[] ids = (String[]) map.get("array");
            StringBuilder sb = new StringBuilder();
            sb.append("DELETE FROM content WHERE id IN (");
            for (int i = 0; i < ids.length; i++) {
                sb.append("'").append(new BigInteger(ids[i]).intValue()).append("'");
                if (i < ids.length - 1)
                    sb.append(",");
            }
            sb.append(")");
            return sb.toString();
        }

    }

}
